package com.example.Lezedata.ui.fragment.commonfragment;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.example.Lezedata.R;
import com.example.Lezedata.ui.fragment.BaseDiaLogFragment;

import common.Common;

@SuppressLint("ResourceAsColor")
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class GenderSelect extends BaseDiaLogFragment {
    public static GenderSelect newInstance() {
        return new GenderSelect();
    }

    public GenderSelect(){}

    public interface confirmListener{
        void selectGender(Common.Gender gender);
    }
    public confirmListener listener;

    private TextView confirm;
    private ImageView close;
    private TextView gender_man;
    private TextView gender_woman;
    private Common.Gender gender = Common.Gender.MALE;
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.gender_select, container, false);
    }

    @Override
    public void initView() {
        super.initView();
        confirm = findViewWithTag("confirm");
        close = findViewWithTag("close");
        gender_man = findViewWithTag("gender_man");
        gender_woman = findViewWithTag("gender_woman");

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmGender();
            }
        });

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        gender_man.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gender = Common.Gender.MALE;
                refreshGender();
            }
        });

        gender_woman.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gender = Common.Gender.FEMALE;
                refreshGender();
            }
        });
    }

    void confirmGender(){
        if (listener != null){
            listener.selectGender(gender);
            dismiss();
        }
    }

    void refreshGender(){
        switch (gender){
            case MALE:
                setSelectGender(gender_man);
                setNoSelectGender(gender_woman);
                break;
            case FEMALE:
                setNoSelectGender(gender_man);
                setSelectGender(gender_woman);
                break;
        }
    }

    void setSelectGender(TextView textView){
        textView.setBackground(getActivity().getDrawable(R.drawable.corner_store_maincolor_10dp));
        textView.setTextColor(R.color.app_color_main);
    }

    void setNoSelectGender(TextView textView){
        textView.setBackground(getActivity().getDrawable(R.drawable.corner_store_darkcolor_10dp));
        textView.setTextColor(R.color.app_color_secondaryText);
    }

    @Override
    public int windowAnimationsType() {
        return R.style.popWindow_anim_style;
    }
}
